﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using NPOI.XWPF.UserModel;

namespace Excel2doc
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //Console---Debug
        string file = "..\\..\\myTest.xlsx";
        private void button1_Click(object sender, EventArgs e)
        {
            
            TestExcelWrite(file);
            TestExcelRead(file);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            NExportTest();
            //ExportTest();
        }
        public void NExportTest()
        {
            List<BookMarks> bList = new List<BookMarks>();

            BookMarks b = new BookMarks();
            b.Name = "Title";
            b.Text = "DocX导出Word Test";
            bList.Add(b);

            b = new BookMarks();
            b.Name = "Content";
            b.Text = "Word正文";
            bList.Add(b);

            b = new BookMarks();
            b.Name = "Bm2";
            b.Text = "Word正文";
            bList.Add(b);
            var fname = "./out/test" + DateTime.Now.ToString("HHmmss") + ".docx";
            NDOC.ToDocx(bList, @"Template.docx", fname);
            Process.Start(fname);
            //var doc = new XWPFDocument();            
            //var p1 = doc.CreateParagraph();
            ////p1.SetAlignment(ParagraphAlignment.LEFT);
            //var r1 = p1.CreateRun();
            //r1.SetBold(true);
            //r1.SetText("The quick brown fox");
            //r1.SetText("Hello2");

            //p1 = doc.CreateParagraph();
            ////p1.SetAlignment(ParagraphAlignment.LEFT);
            //r1 = p1.CreateRun();
            //r1.SetBold(true);
            //r1.SetText("The quick brown fox");
            //r1.SetText("Hello2");

            //FileStream sw = File.OpenWrite("blank.docx");
            //doc.Write(sw);
            //sw.Close();
            //Process.Start("blank.docx");
        }
        //public void ExportTest()
        //{
        //    List<BookMarks> bList = new List<BookMarks>();

        //    BookMarks b = new BookMarks();
        //    b.Name = "Title";
        //    b.Text = "DocX导出Word Test";
        //    bList.Add(b);

        //    b = new BookMarks();
        //    b.Name = "Content";
        //    b.Text = "Word正文";
        //    bList.Add(b);

        //    b = new BookMarks();
        //    b.Name = "Bm2";
        //    b.Text = "Word正文";
        //    bList.Add(b);

        //    var fname = "./out/test" + DateTime.Now.ToString("HHmmss") + ".docx";
        //    ExportWord.ToDocx(bList, @"Template.docx", fname);
        //    Process.Start(fname);

        //}

        static DataTable GenerateData()
        {
            DataTable data = new DataTable();
            for (int i = 0; i < 5; ++i)
            {
                data.Columns.Add("Columns_" + i.ToString(), typeof(string));
            }

            for (int i = 0; i < 10; ++i)
            {
                DataRow row = data.NewRow();
                row["Columns_0"] = "item0_" + i.ToString();
                row["Columns_1"] = "item1_" + i.ToString();
                row["Columns_2"] = "item2_" + i.ToString();
                row["Columns_3"] = "item3_" + i.ToString();
                row["Columns_4"] = "item4_" + i.ToString();
                data.Rows.Add(row);
            }
            return data;
        }

        static void PrintData(DataTable data)
        {
            if (data == null) return;
            for (int i = 0; i < data.Rows.Count; ++i)
            {
                for (int j = 0; j < data.Columns.Count; ++j)
                    Debug.Write(string.Format("{0} ", data.Rows[i][j]));
                Debug.Write("\n");
            }
        }

        static void TestExcelWrite(string file)
        {
            try
            {
                using (ExcelHelper excelHelper = new ExcelHelper(file))
                {
                    DataTable data = GenerateData();
                    int count = excelHelper.DataTableToExcel(data, "MySheet", true);
                    if (count > 0)
                        Debug.WriteLine("Number of imported data is {0} ", count);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception: " + ex.Message);
            }
        }

        static void TestExcelRead(string file)
        {
            try
            {
                using (ExcelHelper excelHelper = new ExcelHelper(file))
                {
                    DataTable dt = excelHelper.ExcelToDataTable("MySheet", true);
                    PrintData(dt);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception: " + ex.Message);
            }
        }

        private void readxls_Click(object sender, EventArgs e)
        {
            try
            {
                openFileDialog1.ShowDialog();
                file = openFileDialog1.FileName;
                if (file == "")
                    return;
                using (ExcelHelper excelHelper = new ExcelHelper(file))
                {
                    dataGridView1.Columns.Clear();
                    DataTable dt = excelHelper.ExcelToDataTable(null, true);
                    dataGridView1.DataSource = dt;
                    DataGridViewCheckBoxColumn newColumn = new DataGridViewCheckBoxColumn(false);
                    dataGridView1.Columns.Insert(0, newColumn);
                    dataGridView1.Columns[0].Name = "选择";
                    dataGridView1.Columns[0].Width = 60;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                //Debug.WriteLine("Exception: " + ex.Message);
            }
        }

        private void seting_Click(object sender, EventArgs e)
        {
            setting s = new setting();
            s.ShowDialog();
        }

        private void button4_Click(object sender, EventArgs e)
        {

            DataTable data = new DataTable("test");
            data.Columns.Add("书签" , typeof(string));
            data.Columns.Add("列名", typeof(string));
            
            DataRow row = data.NewRow();
            row["书签"] = "测试";
            row["列名"] = "列内容测试";
            data.Rows.Add(row);
            row = data.NewRow();
            row["书签"] = "测试111";
            row["列名"] = "列内容测试111";
            data.Rows.Add(row);

            FileStream fs = new FileStream("config.xml", FileMode.Create);
            //DataSet ds = dataGridView1.DataSource as DataSet;                        
            DataSet ds = new DataSet();
            ds.Tables.Add(data);
            ds.WriteXml(fs);
            fs.Close();
            DataSet ds1 = new DataSet();
            ds1.ReadXml("config.xml");
            dataGridView1.DataSource = ds1.Tables[0];

            //datatable 需要写两个文件
            //data.WriteXmlSchema(fs);
            //data.WriteXml(fs);
            //fs.Close();
            //DataTable dt = new DataTable("test");
            //dt.ReadXmlSchema("config.xml");
            //dt.ReadXml("config.xml");
            
        }

        private void writedoc_Click(object sender, EventArgs e)
        {
            try
            {
                int n = 0;
                DataSet ds = new DataSet();
                ds.ReadXml(Application.StartupPath + "\\config.xml");
                DataTable dtset = ds.Tables[0];
                foreach (DataGridViewRow item in dataGridView1.Rows)
                {
                    if (item.Cells[0].Value != null && (bool)item.Cells[0].Value!=false)
                    {                       
                        ExportToDoc(dtset, item);
                        n++;
                    }                    
                }
                MessageBox.Show(string.Format("输出了{0}个文件",n),"提醒");
                if (n>0)
                    Process.Start("explorer", Application.StartupPath+"\\out");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        public void ExportToDoc(DataTable dtset, DataGridViewRow dr)
        {
            List<BookMarks> bList = new List<BookMarks>();
            BookMarks b = new BookMarks();
            foreach (DataRow r in dtset.Rows)
            {
                b = new BookMarks();
                b.Name = r["书签"] as string;
                //b.Text = r["列名"] as string;
                b.Text = dr.Cells[r["列名"].ToString()].Value.ToString() ;
                bList.Add(b);
            }
            var fname = Application.StartupPath + "\\out\\" + dr.Cells[1].Value.ToString() + DateTime.Now.ToString("HHmmss") + ".docx";
            NDOC.ToDocx(bList, Application.StartupPath + "\\Template.docx", fname); 
        }
       
    }
}
